package com.dang.alex.web.servlet.validate;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

@WebServlet("/validateCheckCode.do")
public class VaildateCheckCodeServlet extends HttpServlet{
	
	private static final long serialVersionUID = -7443000991717020562L;
	private boolean flag;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String checkCode = req.getParameter("checkCode");
		try {
			if(checkCode != null && checkCode.equalsIgnoreCase(req.getSession().getAttribute("checkCode").toString())) {
				flag = true;
			}
			else {
				flag = false;
			}
			
			JSONObject json = new JSONObject();
			json.put("flag", flag);
			PrintWriter out = resp.getWriter();
			out.print(json.toString());
			out.close();
		}
		catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
}
